1729B - Decode String - CodeForces Solution


greedy strings

Please click on ads to support us..

Python Code:

t=int(input())
for j in range (t):
    k=int(input())
    L=input()
    A=''
    i=0
    while i<k:
        if i<k-1:
            if (int(L[i+1])!=0):
                                                                            if i<k-2:
                    if int(L[i+2])!=0:
                        b=chr(96+int(L[i]))
                        A=A+b
                        i=i+1
                    else:
                        if i<k-3:
                            if int(L[i+3])!=0:
                                b=chr(96+(10*int(L[i])+int(L[i+1])))
                                A=A+b
                                i=i+3
                            else:
                                b=chr(96+int(L[i]))
                                A=A+b
                                i=i+1
                        else:
                            b=chr(96+(10*int(L[i])+int(L[i+1])))
                            A=A+b
                            i=i+3
                else:
                    b=chr(96+int(L[i]))
                    A=A+b
                    i+=1
            else:
                if i<k-2:
                    if int(L[i+2])!=0:
                        b=chr(96+int(L[i]))
                        A=A+b
                        i=i+1
                    else:
                        if i<k-3:
                            if int(L[i+3])!=0:
                                b=chr(96+(10*int(L[i])+int(L[i+1])))
                                A=A+b
                                i=i+3
                            else:
                                b=chr(96+int(L[i]))
                                A=A+b
                                i=i+1
                        else:
                            b=chr(96+(10*int(L[i])+int(L[i+1])))
                            A=A+b
                            i=i+3
                else:
                    b=chr(96+int(L[i]))
                    A=A+b
                    i+=1
        elif L[k-1]!=0:
                b=chr(96+int(L[i]))
                A=A+b
                i+=1
        else:
                i+=1
    print(A)

C++ Code:

//BISMILLAHIR RAHMANIR RAHIM
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>

using namespace std;

typedef long long                        ll;
typedef unsigned long long               llu;
typedef long double                      ld;
typedef vector<int>                      vi;
typedef vector<ll>                       vl;
typedef vector<double>                   vd;
typedef vector<vi>                       vvi;
typedef vector<vl>                       vvl;
typedef pair<int,int>                    pii;
typedef pair<ll, ll>                     pll;
typedef pair<double,double>              pdd;
typedef vector<pii>                      vpi;
typedef vector<pll>                      vpl;
typedef vector<pdd>                      vpd;


#define fr(n)      for(int i=0;i<n;i++)
#define fr1(n)    for(int i=1;i<=n;i++)
#define frr(n)     for(int i=n;i>0;i--)
#define frr1(n)   for(int i=n;i>=0;i--)


#define pb        push_back
#define pf        push_front
#define epb       emplace_back
#define epf       emplace_front
#define pob       pop_back
#define pof       pop_front
#define ins       insert
#define rev       reverse
#define mp        make_pair
#define um        unordered_multiset
#define us        unordered_set
#define lb        lower_bound
#define ub        upper_bound
#define ff        first
#define ss        second
#define bb        break
#define cc        continue
#define el        "\n"
#define r0        return 0


#define kase                        int tc; cin>>tc; for(int z=1;z<=tc;z++)
#define case_print                  cout<<"Case "<<z<<": "
#define dcml(x)                     cout<<fixed<<setprecision(x)
#define sz(x)                       (int )x.size()
#define ele(arr)                    sizeof(arr)/sizeof(arr[0])
#define all(x)                      (x).begin(),(x).end()
#define allr(x)                     (x).rbegin(), (x).rend()
#define alla(x,sz)                  (x),(x+sz)
#define sortd(x)                    sort(all(x), greater<int>())
#define sortda(x,sz)                sort(alla(x,sz), greater<int>())
#define make_unique(x)              sort(all(x)); (x).erase(unique(all(x)) ,(x).end());
#define sqr(x)                      ((ll)(x)*(x))
#define pqmin(x)                    priority_queue<x, vector<x>, greater< x > >
#define pq(x)                       priority_queue<x>

#define maxe                       *max_element
#define mine                       *min_element
#define Sum(x)                      accumulate(all(x),0)
#define bs                          binary_search
#define mem(x,y)                    memset(x,y,sizeof(x))
#define memclr(x,y)                 memset(x,0,sizeof(x))

#define gcd(a, b)                    __gcd(a, b)
#define lcm(a, b)                   ((a)*((b)/gcd(a,b)))

#define mod                          1000000007
#define err                          1e-9
#define pi                           (2*acos ( 0 ) )
#define PI                           3.141592653589793
#define optimize()                   ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FILE                         freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);


int nxt()
{
    int x;
    cin>>x;
    return x;
}
long long lxt()
{
    long long x;
    cin>>x;
    return x;
}
double dxt()
{
    double x;
    cin>>x;
    return x;
}
long double ldxt()
{
    long double x;
    cin>>x;
    return x;
}


bool cmp(const pair<int, int> &p, const pair<int, int> &q)
{
    if(p.first<q.first) return 1;
    else if(p.first == q.first) return (p.second <q.second);
    else
        return 0;
//AA
}

int main()
{
    // FILE
    optimize()
       kase{
        int len = nxt();
        vector<char>v;
        string s;
        cin>>s;
        for(int i = len-1; i>=0; i--){
            if(s[i] == '0'){
                int x = s[i-1] -48;
                int y = s[i-2] -48;
                int n = y*10 +x;
                char c = 96+n;
                v.epb(c);
                i-=2;
            }
            else{
                int x = s[i] -48;
                char ck = x+96;
                v.emplace_back(ck);
            }
        } 

        reverse(all(v));
        for(auto u : v){
            cout<<u;
        }
        cout<<el;
        
       }
    r0;
}


Comments

Submit
0 Comments
More Questions

791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II